/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lojavirtual.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import lojavirtual.model.Role;
import lojavirtual.model.Usuario;
import lojavirtual.util.ConnectionFactory;

/**
 *
 * @author 710930
 */
public class RoleDAO implements DAO<Role>{

    public void inserir(Role role) {
        Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = " INSERT INTO ROLE(LOGIN, ROLE) VALUES (?, ?) ";
           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, role.getUsuario().getLogin());
           statement.setString(2, role.getRole());

           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public void alterar(Role role) {
        Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = "UPDATE ROLE (ROLE) SET (?) WHERE LOGIN = ?";

           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, role.getRole());
           statement.setString(2, role.getUsuario().getLogin());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public List<Role> listar(Role e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remover(Role role) {
         Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = "DELETE FROM ROLE WHERE LOGIN = ?";

           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, role.getUsuario().getLogin());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public Role tipoacesso(Usuario usuario){
        Connection connection = ConnectionFactory.getConnection();
        Role role = new Role();
       try{

           String SQL = " SELECT * FROM ROLE WHERE LOGIN = '" + usuario.getLogin()+"'" ;

           Statement statement = connection.createStatement();
           ResultSet rs = statement.executeQuery(SQL);
           rs.next();

           String acesso = rs.getString("ROLE");
          
           role.setRole(acesso);
           role.setUsuario(usuario);

           return role;
           
       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }

       return role;
    }
}
